3 use MediaWiki\Shell\Command
;
8 class CommandTest
extends PHPUnit_Framework_TestCase
{
10 * @expectedException PHPUnit_Framework_Error_Notice
12 public function testDestruct() {
13 if ( defined( 'HHVM_VERSION' ) ) {
14 $this->markTestSkipped( 'destructors are unreliable in HHVM' );
16 $command = new Command();
17 $command->params( 'true' );
20 private function requirePosix() {
21 if ( wfIsWindows() ) {
22 $this->markTestSkipped( 'This test requires a POSIX environment.' );
27 * @dataProvider provideExecute
29 public function testExecute( $commandInput, $expectedExitCode, $expectedOutput ) {
30 $this->requirePosix();
32 $command = new Command();
34 ->params( $commandInput )
37 $this->assertSame( $expectedExitCode, $result->getExitCode() );
38 $this->assertSame( $expectedOutput, $result->getStdout() );
41 public function provideExecute() {
43 'success status' => [ 'true', 0, '' ],
44 'failure status' => [ 'false', 1, '' ],
45 'output' => [ [ 'echo', '-n', 'x', '>', 'y' ], 0, 'x > y' ],
49 public function testEnvironment() {
50 $this->requirePosix();
52 $command = new Command();
54 ->params( [ 'printenv', 'FOO' ] )
55 ->environment( [ 'FOO' => 'bar' ] )
57 $this->assertSame( "bar\n", $result->getStdout() );
60 public function testStdout() {
61 $this->requirePosix();
63 $command = new Command();
66 ->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' )
69 $this->assertNotContains( 'ThisIsStderr', $result->getStdout() );
70 $this->assertEquals( "ThisIsStderr\n", $result->getStderr() );
73 public function testStdoutRedirection() {
74 $this->requirePosix();
76 $command = new Command();
79 ->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' )
80 ->includeStderr( true )
83 $this->assertEquals( "ThisIsStderr\n", $result->getStdout() );
84 $this->assertNull( $result->getStderr() );
87 public function testOutput() {
90 $this->requirePosix();
93 $command = new Command();
95 ->params( [ 'ls', 'index.php' ] )
97 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
98 $this->assertSame( null, $result->getStderr() );
100 $command = new Command();
102 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
105 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
106 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStdout() );
107 $this->assertSame( null, $result->getStderr() );
109 $command = new Command();
111 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
113 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
114 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStderr() );
117 public function testT69870() {
118 $commandLine = wfIsWindows()
120 ?
( 'for /l %i in (1, 1, 1001) do @echo ' . str_repeat( '*', 331 ) )
121 : 'printf "%-333333s" "*"';
123 // Test several times because it involves a race condition that may randomly succeed or fail
124 for ( $i = 0; $i < 10; $i++
) {
125 $command = new Command();
126 $output = $command->unsafeParams( $commandLine )
129 $this->assertEquals( 333333, strlen( $output ) );